package cn.m1c.cntt.wuhan2020.cnttlifegoods.order.service


import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.enums.StatusCode
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.model.ResultVo
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.utils.AssertUtil
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.utils.DateUnit
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.utils.plus
import cn.m1c.cntt.wuhan2020.cnttlifegoods.order.dao.OrderBuyerRepository
import cn.m1c.cntt.wuhan2020.cnttlifegoods.order.dao.OrderRepository
import cn.m1c.cntt.wuhan2020.cnttlifegoods.order.vo.OrderBuyerVo
import cn.m1c.cntt.wuhan2020.cnttlifegoods.order.vo.OrderVo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.stereotype.Service
import java.lang.AssertionError
import java.util.*
import javax.annotation.Resource
import kotlin.collections.HashMap


@Service
class OrderService {
    @Autowired
    lateinit var orderRepository: OrderRepository
    @Autowired
    lateinit var orderBuyerRepository: OrderBuyerRepository

    @Resource
    lateinit var redisTemplate : RedisTemplate<String, OrderVo>

    fun save(orderVo: OrderVo): ResultVo<HashMap<String, String>> {
        var resultVo = ResultVo<HashMap<String,String>>(data = HashMap())
        AssertUtil.hasLength(orderVo.phone,StatusCode.failed,"手机号为空")
        AssertUtil.isNotNull(orderVo.areaList,StatusCode.failed,"覆盖小区为空")
        AssertUtil.isNotNull(orderVo.goodsList,StatusCode.failed,"物资为空")
        orderVo.endDate = Date().plus(1)
        orderRepository.save(orderVo)
        return resultVo
    }

    fun list(orderVo: OrderVo): List<OrderVo> {
        var list = orderRepository.findAll()
        return list
    }

    fun findById(id:String): OrderVo {
        var orderVo = orderRepository.findById(id).get()
        return orderVo
    }

    fun saveBuyer(orderBuyerVo: OrderBuyerVo){
        //删除旧的，保存新的
        deleteOrderBuyer(orderBuyerVo.userId!!,orderBuyerVo.orderId!!)
        orderBuyerRepository.save(orderBuyerVo)
    }

    fun findBuyer(): List<OrderBuyerVo> {
        var orderBuyerVoList = orderBuyerRepository.findAll()
        return orderBuyerVoList
    }

    fun findBuyerByUserIdAndOrderId(userId:String,orderId:String): OrderBuyerVo {
        var orderBuyerVo = orderBuyerRepository.findOrderBuyerVoByUserIdAndOrderId(userId,orderId)?.get(0)
        return orderBuyerVo
    }
    fun deleteOrderBuyer(userId:String,orderId:String) {
        var orderBuyerVo = orderBuyerRepository.findOrderBuyerVoByUserIdAndOrderId(userId,orderId)?.get(0)
        if(orderBuyerVo!=null){
            orderBuyerRepository.deleteById(orderBuyerVo?.id!!)
        }
    }

    fun updateOrderStatus(orderVo: OrderVo,userId: String) {
        var orderVoData = findById(orderVo.id!!)
        AssertUtil.isTrue(orderVoData.userId.equals(userId),StatusCode.failed,"您没有权限操作该采购活动")
        AssertUtil.isTrue(orderVo.orderStatus.compareTo(orderVoData.orderStatus)>0,StatusCode.failed,"状态不可退回")
        orderVoData.orderStatus = orderVo.orderStatus
        orderRepository.save(orderVoData)
    }

}